#include "ranking_list.h"
#include "ui_ranking_list.h"

ranking_List::ranking_List(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::ranking_List)
{
    ui->setupUi(this);
    QImage image1;
    image1.load(":/img/ranking.jpg");
    QImage image2=image1.scaled(680,381);

    QPalette palette;
    palette.setBrush(this->backgroundRole(),QBrush(image2));//设置背景
    setPalette(palette);
    ranking();
    for(int i=1;i<UserList::userlist->size();i++)
    {

        double a,b,c;
        a = atof(UserList::userlist->at(i)->number.c_str());
        b = atof(UserList::userlist->at(i)->winNumber.c_str());
        c = (b/a)*100;
        if(b== 0){
            c = 0;
        }
        QString d;
        d =  QString::number(c,10,2);
        ui->tableWidget->setItem(i-1,0,new QTableWidgetItem(QString::fromStdString(UserList::userlist->at(i)->name)));
        ui->tableWidget->setItem(i-1,1,new QTableWidgetItem(QString::fromStdString(UserList::userlist->at(i)->number)));
        ui->tableWidget->setItem(i-1,2,new QTableWidgetItem(QString::fromStdString(UserList::userlist->at(i)->winNumber)));
        ui->tableWidget->setItem(i-1,3,new QTableWidgetItem(d + "%"));
    }
}

ranking_List::~ranking_List()
{
    delete ui;
}

void ranking_List::ranking()
{
    for(int i=1;i<UserList::userlist->size();i++)
    {
        for(int j=1;j<UserList::userlist->size()-i-1;j++)
        {
            double a1[2],b1[2];
            a1[0] = atof(UserList::userlist->at(j)->number.c_str());
            b1[0] = atof(UserList::userlist->at(j)->winNumber.c_str());
            a1[1] = atof(UserList::userlist->at(j+1)->number.c_str());
            b1[1] = atof(UserList::userlist->at(j+1)->winNumber.c_str());
            if((b1[0]/a1[0])<(b1[1]/a1[1]))
            {
                swap(UserList::userlist->at(j),UserList::userlist->at(j+1));
            }
        }
    }
}
